package com.twosnail.frame.controller;

import com.jfinal.core.Controller;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.twosnail.frame.commin.util.result.ResultObj;
import com.twosnail.frame.model.IdentifyingCode;
import com.twosnail.frame.model.WxUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * Created by yangcs on 2017/7/20.
 */
public class IdentifyingCodeController extends Controller {

    private Logger logger = Logger.getLogger(IdentifyingCodeController.class) ;

    @RequiresPermissions("IdentifyingCodeController")
    public void index(){
        try {
            Integer pageNum = getParaToInt( "pageNum" ) ;
            Integer numPerPage = getParaToInt( "numPerPage" ) ;
            String keyWord = getPara("keyWord") ;
            numPerPage = ( numPerPage == null || numPerPage == 0 ) ? 5 : numPerPage;
            pageNum = pageNum == null ? 1 : pageNum;
            Page<Record> list = IdentifyingCode.me.getAllCode(pageNum,numPerPage,keyWord);
            setAttr( "list", list );
            setAttr( "keyWord", keyWord );
        } catch (Exception e) {
            this.logger.warn( "用户列表信息，初始化失败！" , e );
        }
        render( "ic_list.html" );
    }

    /**
     * 添加代金劵
     */
    @RequiresPermissions("IdentifyingCodeController.addCode")
    public void addCode(){
        String mobile  = getPara( "mobile" ) ;
        int count  = Integer.parseInt(getPara( "count" )) ;
        String days = getPara( "days" );
        try {
            if( mobile == null || "".equals( mobile ) ) {
                renderJson( new ResultObj( ResultObj.FAIL , "手机号不能为空！" , null ) );
            } else {
                WxUser wxUser = WxUser.me.getWxUser(mobile);
                if( wxUser == null ){
                    renderJson( new ResultObj( ResultObj.FAIL , "手机号未注册！" , null ) );
                }else{
                    IdentifyingCode.me.addCodes(wxUser.getLong("wxUserId"),count,mobile,days);
                    renderJson( new ResultObj( ResultObj.SUCCESS , "添加成功" , days ) );
                }
            }
        } catch ( Exception e ) {
            this.logger.info( "系统异常，添加代金劵失败！", e );
            renderJson( new ResultObj( ResultObj.FAIL , e.getMessage() , null ) );
        }
    }

    /**
     * 添加vip
     */
    public void addVip(){
        String mobile  = getPara( "mobile" ) ;
        try {
            if( mobile == null || "".equals( mobile ) ) {
                renderJson( new ResultObj( ResultObj.FAIL , "手机号不能为空！" , null ) );
            } else {
                WxUser wxUser = WxUser.me.getWxUser(mobile);
                if( wxUser == null ){
                    renderJson( new ResultObj( ResultObj.FAIL , "手机号未注册！" , null ) );
                }else if( wxUser.getInt("permission")!=null && wxUser.getInt("permission") == 2 ){
                    renderJson( new ResultObj( ResultObj.FAIL , "该用户已经是vip用户！" , null ) );
                }else{
                    IdentifyingCode.me.addVip(wxUser.getLong("wxUserId"),mobile);
                    renderJson( new ResultObj( ResultObj.SUCCESS , "添加成功" , null ) );
                }
            }
        } catch ( Exception e ) {
            this.logger.info( "系统异常，添加代金劵失败！", e );
            renderJson( new ResultObj( ResultObj.FAIL , e.getMessage() , null ) );
        }
    }
}
